import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Node extends Component {
  static propTypes = {
    node: PropTypes.object.isRequired,
    renderMap: PropTypes.object.isRequired,
  };

  renderChild = node => {
    const { injectProps, renderMap } = this.props;
    return (
      <Node
        key={node.key}
        node={node}
        injectProps={injectProps}
        renderMap={renderMap}
      />
    );
  };

  render() {
    const { node, injector, renderMap } = this.props;
    const renderer = renderMap[node.type];
    const nodes = node.nodes || [];
    const children = nodes.map(this.renderChild);
    const props = injector.inject({ ...this.props, children });
    const renderNode = () => {
      if (typeof renderer === 'function') {
        return renderer(props);
      } else{
        return <div />;
      }
    };
    const content = renderNode();
    return content;
  }
}

export default Node;
